<template>
  <a-layout>
    <a-layout-sider width="200" style="background: #fff">
      <a-menu
          v-model:selectedKeys="selectedKeys2"
          v-model:openKeys="openKeys"
          mode="inline"
          :style="{ height: '100%', borderRight: 0 }"
          @click="handleClick"
      >
        <a-menu-item key="welcome">
          <MailOutLined/>
          <span>Welcome</span>
        </a-menu-item>

        <a-sub-menu v-for="item in level1" :key="item.id">
          <template v-slot:title>
            <span><user-outlined/>{{ item.name }}</span>
          </template>
          <a-menu-item v-for="child in item.children" :key="child.id">
            <BookOutlined/>
            <span>{{ child.name }}</span>
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <div class="welcome" v-show="isShowWelcome">
        <the-welcome></the-welcome>
      </div>
      <a-list v-show="!isShowWelcome" item-layout="vertical" size="large" :pagination="pagination" :data-source="ebooks"
              :grid="{ gutter: 20, column: 3 }">
        <template #renderItem="{ item }">
          <a-list-item key="item.name">
            <template #actions>
          <span>
            <component v-bind:is="'file-two-tone'" style="margin-right: 8px"/>
            {{ item.docCount }}
          </span>
          <span>
            <component v-bind:is="'like-two-tone'" style="margin-right: 8px"/>
            {{ item.voteCount }}
          </span>
          <span>
            <component v-bind:is="'fire-two-tone'" style="margin-right: 8px"/>
            {{ item.viewCount }}
          </span>
            </template>
            <a-list-item-meta :description="item.description">
              <template #title>
                <router-link :to="'/doc?ebookId=' + item.id">
                  {{ item.name }}
                </router-link>
              </template>
              <template #avatar>
                <a-avatar :src="item.cover"/>
              </template>
            </a-list-item-meta>
          </a-list-item>
        </template>
      </a-list>
    </a-layout-content>
  </a-layout>
</template>

<script lang="ts">
import {defineComponent, onMounted, ref} from 'vue';
import axios from 'axios';
import {message} from "ant-design-vue";
import {Tool} from "@/util/tool";
import TheWelcome from "@/components/the-welcome.vue"

export default defineComponent({
  name: 'Home',
  components: {TheWelcome},
  setup() {
    const ebooks = ref(); // Vue3新增了ref用来定义响应式数据，需要import进来。
    const pagination = ref({
      current: 1,
      pageSize: 100,
      total: 0
    });
    const level1 = ref();
    let categorys: any;
    const handleQueryCategory = () => {
      axios.get("/category/all").then((response) => {
        const data = response.data;
        if (data.success) {
          categorys = data.content; // categorys的数据在content
          console.log("原始数组：", categorys);
          level1.value = [];
          level1.value = Tool.array2Tree(categorys, 0);
          console.log("树形结构：", level1);
        } else {
          message.error(data.message);
        }
      })
    }
    const isShowWelcome = ref(true);
    let categoryId2 = 0;
    const handleClick = (value: any) => {
      console.log("menu click", value);
      if (value.key === 'welcome') {
        isShowWelcome.value = true;
      } else {
        isShowWelcome.value = false;
        categoryId2 = value.key;
        handleQuery({ // 通过下面的数据传给方法里面的params，params通过axios传给后端
          page: 1, // 加载第一页，这里的page要和后端接收的类中的属性名保持一致。
          size: pagination.value.pageSize, // 用pagination的原因：ui分页组件里面对应的参数就叫pagination,里面的属性也是pageSize。其他同上，保持一致。
          categoryId2: categoryId2
        });
      }
    }
    const handleQuery = (params: any) => {
      axios.get("/ebook/list", {
        params: {
          page: params.page,
          size: params.size,
          categoryId2: params.categoryId2
        }
      }).then((response) => {
        const data = response.data; // 这里的data就等于后端传来的CommonResp中相对应的结构
        ebooks.value = data.content.list; //ref赋的值是xxx.value
        // ebooks2.books = data.content;
        console.log(response);
        // 重置分页按钮
        pagination.value.current = params.page;
        pagination.value.total = data.content.total;
      });
    }
    // const ebooks2 = reactive({books:[]});
    onMounted(() => {
      handleQuery({ // 通过下面的数据传给方法里面的params，params通过axios传给后端
        page: 1, // 加载第一页，这里的page要和后端接收的类中的属性名保持一致。
        size: pagination.value.pageSize, // 用pagination的原因：ui分页组件里面对应的参数就叫pagination,里面的属性也是pageSize。其他同上，保持一致。
        CategoryId2: categoryId2
      });
      handleQueryCategory();
    });


    /**
     * 表格点击页码时触发
     */
    const handleTableChange = (pagination: any) => {
      console.log("看看自带的分页参数都有什么：" + pagination);
      handleQuery({ // 通过下面的数据传给方法里面的params，params通过axios传给后端
        page: 1, // 加载第一页，这里的page要和后端接收的类中的属性名保持一致。
        size: pagination.value.pageSize, // 用pagination的原因：ui分页组件里面对应的参数就叫pagination,里面的属性也是pageSize。其他同上，保持一致。
        CategoryId2: categoryId2
      });
    }

    return {
      ebooks,
      // books:toRef(ebooks2, "books")
      // listData,
      // pagination: {
      //   onChange: (page: any) => {
      //     console.log(page);
      //   },
      //   pageSize: 5,
      // },
      pagination,
      actions: [
        {type: 'starOutlined', text: '156'},
        {type: 'LikeOutlined', text: '156'},
        {type: 'MessageOutlined', text: '2'},
      ],
      handleTableChange,
      level1,
      handleClick,
      isShowWelcome
    }
  }
});


</script>
<!--
scoped:表示这里的样式只在当前组件起作用
-->
<style scoped>
.ant-avatar {
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 8%;
  margin: 5px 0;
}
</style>
